package com.share.example.selenium.utils;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class UsDateUtil {
    // 时区
    public static final ZoneId zoneId = ZoneId.of("America/New_York");
    public static final TimeZone timeZone = TimeZone.getTimeZone(zoneId);
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String HHMMSS = "HHmmss";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {
        String datetime = "2022-11-11 08:12:16";
        Date date = new Date();
        System.out.println(addHourToDate(date, 12));
        System.out.println(strDT2Date(datetime));
    }

    private static SimpleDateFormat getSimpleDateFormat(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        sdf.setTimeZone(timeZone);
        return sdf;
    }

    private static Calendar getCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(timeZone);
        return cal;
    }

    private static Calendar getCalendar(Date date) {
        Calendar cal = getCalendar();
        cal.setTime(date);
        return cal;
    }

    public static Date addHourToDate(Date date, int hour) {
        Calendar calendar = getCalendar(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    public static Date strD2Date(String strDate) {
        String s = strDate.replaceAll(" ", "").replaceAll("-", "").replaceAll(":", "");
        try {
            Date parse = getSimpleDateFormat(YYYYMMDD).parse(s);
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date strDT2Date(String strDateTime) {
        String s = strDateTime.replaceAll(" ", "").replaceAll("-", "").replaceAll(":", "");
        try {
            Date parse = getSimpleDateFormat(YYYYMMDDHHMMSS).parse(s);
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getHourFromTime(String strTime) {
        String s = strTime.substring(0, 2);
        return Integer.parseInt(s);
    }

    public static int getHour(Date date) {
        Calendar calendar = getCalendar(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static String toYYYY_MM_DD(Date inputDate) {
        return getSimpleDateFormat(YYYY_MM_DD).format(inputDate);
    }

    public static String toHH_MM_SS(Date inputDate) {
        return getSimpleDateFormat(HH_MM_SS).format(inputDate);
    }

    public static String toYYYYMMDD(Date inputDate) {
        return getSimpleDateFormat(YYYYMMDD).format(inputDate);
    }

    public static String toHHMMSS(Date inputDate) {
        return getSimpleDateFormat(HHMMSS).format(inputDate);
    }

    public static String toYYYYMMDDHHMMSS(Date inputDate) {
        return getSimpleDateFormat(YYYYMMDDHHMMSS).format(inputDate);
    }

    public static String toYYYY_MM_DD_HH_MM_SS(Date inputDate) {
        return getSimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(inputDate);
    }

    public static String toMMDDHH(Date usStartDateTime) {
        return toYYYYMMDDHHMMSS(usStartDateTime).substring(4, 10);
    }
}
